/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.zhuike.novel.app.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.umeng.analytics.MobclickAgent;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.BookMixAToc;
import net.zhuike.novel.app.bean.BookSit;
import net.zhuike.novel.app.ui.adapter.TocListAdapter;
import net.zhuike.novel.app.ui.adapter.TocSourceListAdapter;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import framework.common.baseui.UeBaseActivity;

/**
 * @author yuyh.
 * @date 2016/9/8.
 */
public class BookSourceActivity extends UeBaseActivity {

    public static final String INTENT_BOOK_ID = "bookId";
    private Context mContext;
    @Bind(R.id.menu_recycler_view)
    ListView easyRecyclerView;
    private TocSourceListAdapter mTocListAdapter;
    public static void start(Activity activity, List<BookSit.Chapters> chaptersList, int reqId,String bookId) {
        activity.startActivityForResult(new Intent(activity, BookSourceActivity.class).putExtra("bookIds",bookId)
                .putExtra(INTENT_BOOK_ID, (Serializable) chaptersList), reqId);
    }
    private List<BookSit.Chapters> mChapterList = new ArrayList<>();
    private String bookId = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_read_souce);
        showGoBackBtn();
        hiddenFooter();
        setTitleText("选择来源");
        mContext=this;
        initDatas();
    }

    public void initDatas() {
        bookId=getIntent().getStringExtra("bookIds");
        mChapterList=(List<BookSit.Chapters>)getIntent().getSerializableExtra(INTENT_BOOK_ID);
        if(mChapterList!=null) {
            mTocListAdapter = new TocSourceListAdapter(this, mChapterList, bookId);
            easyRecyclerView.setAdapter(mTocListAdapter);
            easyRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    BookSit.Chapters data=mChapterList.get(i);

                    SharedPreferencesUtil.getInstance().putString(bookId+"source"+i,i+"");
                    for(int j=0;j<mChapterList.size();j++){
                        if(SharedPreferencesUtil.getInstance().getString(bookId+"source"+i,"").equals(String.valueOf(j))){

                        } else{
                            SharedPreferencesUtil.getInstance().putString(bookId+"source"+j,"");
                        }
                    }
                    setResult(RESULT_OK,new Intent().putExtra("source",data));
                    finish();
                }
            });
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("书籍源");
        MobclickAgent.onResume(context);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("书籍源");
        MobclickAgent.onPause(context);
    }
}
